import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _regeneratorRuntime from "@babel/runtime/regenerator";
var _excluded = ["tenantId", "employeeId", "customizeUnitCode"];

/**
 * model 员工定义
 * @date: 2018-6-27
 */
import { createPagination, getResponse } from 'utils/utils';
import { queryMapIdpValue } from 'cpaas-front/lib/services/api';
import { addEmployee, deleteUser as _deleteUser, fetchPosition as _fetchPosition, fetchUser as _fetchUser, renderTreeData, saveEmployee, searchEmployee, searchEmployeeById, searchPositionTree, searchUserList, updatePosition as _updatePosition, updateUser as _updateUser } from "../services/employeeService";
export default {
  namespace: 'employee',
  state: {
    list: [],
    // 员工信息列表
    positions: [],
    // 岗位信息（树形结构）
    users: {},
    // 用户信息(含分页信息)
    pagination: {},
    // 分页参数
    employeeInfo: {},
    // 员工信息明细
    pathMap: {},
    // 岗位tree中层级结构信息
    expandedRowKeys: [],
    // 默认展开的岗位Id列表
    positionList: [],
    // 已分配岗位信息列表
    userList: [],
    // 已分配的用户信息
    lov: {} // 存放查询到的值集

  },
  effects: {
    // 获取值集
    fetchEnum: /*#__PURE__*/_regeneratorRuntime.mark(function fetchEnum(_, _ref) {
      var call, put, res, safeRes;
      return _regeneratorRuntime.wrap(function fetchEnum$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryMapIdpValue, {
                employeeStatus: 'CPFM.EMPLOYEE_STATUS'
              });

            case 3:
              res = _context.sent;
              safeRes = getResponse(res);

              if (!safeRes) {
                _context.next = 8;
                break;
              }

              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  lov: {
                    employeeStatus: safeRes.employeeStatus
                  }
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, fetchEnum);
    }),
    fetchEmployeeData: /*#__PURE__*/_regeneratorRuntime.mark(function fetchEmployeeData(_ref2, _ref3) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchEmployeeData$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(searchEmployee, payload);

            case 5:
              _context2.t1 = _context2.sent;
              result = (0, _context2.t0)(_context2.t1);

              if (!result) {
                _context2.next = 10;
                break;
              }

              _context2.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  list: result.content,
                  pagination: createPagination(result),
                  employeeInfo: {}
                }
              });

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchEmployeeData);
    }),
    // 创建员工
    saveEmployee: /*#__PURE__*/_regeneratorRuntime.mark(function saveEmployee(_ref4, _ref5) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function saveEmployee$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call;
              _context3.next = 4;
              return call(addEmployee, payload);

            case 4:
              result = _context3.sent;
              return _context3.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, saveEmployee);
    }),
    // 获取员工明细信息
    fetchDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDetail(_ref6, _ref7) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchDetail$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call, put = _ref7.put;
              _context4.next = 4;
              return call(searchEmployeeById, payload);

            case 4:
              result = _context4.sent;
              result = getResponse(result);

              if (!result) {
                _context4.next = 9;
                break;
              }

              _context4.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  employeeInfo: result
                }
              });

            case 9:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchDetail);
    }),
    // 获取员工已分配的岗位信息
    fetchPosition: /*#__PURE__*/_regeneratorRuntime.mark(function fetchPosition(_ref8, _ref9) {
      var payload, call, put, tenantId, employeeId, customizeUnitCode, otherProps, result;
      return _regeneratorRuntime.wrap(function fetchPosition$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call, put = _ref9.put;
              tenantId = payload.tenantId, employeeId = payload.employeeId, customizeUnitCode = payload.customizeUnitCode, otherProps = _objectWithoutProperties(payload, _excluded);
              _context5.next = 5;
              return call(_fetchPosition, {
                tenantId: tenantId,
                employeeId: employeeId,
                customizeUnitCode: customizeUnitCode
              });

            case 5:
              result = _context5.sent;
              result = getResponse(result);

              if (!result) {
                _context5.next = 10;
                break;
              }

              _context5.next = 10;
              return put({
                type: 'updateState',
                payload: _objectSpread({
                  positionList: result
                }, otherProps)
              });

            case 10:
            case "end":
              return _context5.stop();
          }
        }
      }, fetchPosition);
    }),
    // 获取员工已分配的用户
    fetchUser: /*#__PURE__*/_regeneratorRuntime.mark(function fetchUser(_ref10, _ref11) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchUser$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call, put = _ref11.put;
              _context6.t0 = getResponse;
              _context6.next = 5;
              return call(_fetchUser, payload);

            case 5:
              _context6.t1 = _context6.sent;
              result = (0, _context6.t0)(_context6.t1);

              if (!result) {
                _context6.next = 10;
                break;
              }

              _context6.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  userList: result
                }
              });

            case 10:
            case "end":
              return _context6.stop();
          }
        }
      }, fetchUser);
    }),
    // 员工明细保存兼主岗信息
    saveData: /*#__PURE__*/_regeneratorRuntime.mark(function saveData(_ref12, _ref13) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function saveData$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call;
              _context7.next = 4;
              return call(saveEmployee, payload);

            case 4:
              result = _context7.sent;
              return _context7.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, saveData);
    }),
    // 获取岗位信息(树形结构)
    searchPosition: /*#__PURE__*/_regeneratorRuntime.mark(function searchPosition(_ref14, _ref15) {
      var payload, call, put, result, pathMap;
      return _regeneratorRuntime.wrap(function searchPosition$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              call = _ref15.call, put = _ref15.put;
              _context8.next = 4;
              return call(searchPositionTree, payload);

            case 4:
              result = _context8.sent;
              result = getResponse(result) || [];
              pathMap = renderTreeData(result, {});

              if (!result) {
                _context8.next = 10;
                break;
              }

              _context8.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  pathMap: pathMap,
                  positions: result // expandedRowKeys: Object.keys(pathMap),

                }
              });

            case 10:
            case "end":
              return _context8.stop();
          }
        }
      }, searchPosition);
    }),
    // 更新员工岗位信息
    updatePosition: /*#__PURE__*/_regeneratorRuntime.mark(function updatePosition(_ref16, _ref17) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function updatePosition$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref16.payload;
              call = _ref17.call;
              _context9.next = 4;
              return call(_updatePosition, payload);

            case 4:
              result = _context9.sent;
              return _context9.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, updatePosition);
    }),
    // 查询可分配用户信息列表
    searchUser: /*#__PURE__*/_regeneratorRuntime.mark(function searchUser(_ref18, _ref19) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function searchUser$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref18.payload;
              call = _ref19.call, put = _ref19.put;
              _context10.t0 = getResponse;
              _context10.next = 5;
              return call(searchUserList, payload);

            case 5:
              _context10.t1 = _context10.sent;
              result = (0, _context10.t0)(_context10.t1);

              if (!result) {
                _context10.next = 10;
                break;
              }

              _context10.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  users: result
                }
              });

            case 10:
            case "end":
              return _context10.stop();
          }
        }
      }, searchUser);
    }),
    // 移除员工已分配用户信息
    deleteUser: /*#__PURE__*/_regeneratorRuntime.mark(function deleteUser(_ref20, _ref21) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteUser$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref20.payload;
              call = _ref21.call;
              _context11.next = 4;
              return call(_deleteUser, _objectSpread({}, payload));

            case 4:
              result = _context11.sent;
              return _context11.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context11.stop();
          }
        }
      }, deleteUser);
    }),
    // 更新用户已分配用户信息
    updateUser: /*#__PURE__*/_regeneratorRuntime.mark(function updateUser(_ref22, _ref23) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function updateUser$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              payload = _ref22.payload;
              call = _ref23.call;
              _context12.next = 4;
              return call(_updateUser, _objectSpread({}, payload));

            case 4:
              result = _context12.sent;
              return _context12.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context12.stop();
          }
        }
      }, updateUser);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref24) {
      var payload = _ref24.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};